package plgdb

import (
	"dgo/goutils/utils"
	"fmt"
	"gitee.com/ymofen/supervalue"
	"strings"
	"time"
)

func (this *PlgDBBase) PostASyncExec() {

}

func (this *PlgDBBase) checkProcessVar(val *string, gvfn TGetParamValFunc) bool {
	str := *val

	for {
		id := utils.GetBetween(str, "$", "$", 0)
		if len(id) == 0 {
			break
		}

		replkey := fmt.Sprintf("$%s$", id)
		ok, v1 := gvfn(id)
		v1str := utils.GetStrValue(v1, "")
		if !ok {
			if utils.CompareIgnoreCase(id, "now") == 0 {
				v1str = utils.DateTimeString2(time.Now())
			} else if utils.CompareIgnoreCase(id, "date") == 0 {
				v1str = utils.DateString(time.Now())
			} else if utils.CompareIgnoreCase(id, "newid") == 0 {
				v1str = utils.RandKeyString(24)
			} else if utils.CompareIgnoreCase(id, "rand_8_key") == 0 {
				v1str = utils.RandKeyString(8)
			} else if utils.CompareIgnoreCase(id, "randnum_4") == 0 {
				v1str = utils.RandNumStringWithout4(4)
			}
		}
		str = strings.ReplaceAll(str, replkey, v1str)
	}
	*val = str
	return true
}

/*
*

	"check":{
	    "userid":{
	       "value":"$$NOTEMPTY$$",      // $$NOTEMPTY$$ 表示不能为空, 也可以取用户id(多个用逗号分割), (需要配合GETVAR回调使用)
	       "errcode":101, "msg":""      // 验证不通过响应错误代码和消息
	    }
	 },
*/
func (this *PlgDBBase) checkVarByResConf(conf *wrapper.SuperValue, gvfn TGetParamValFunc) (ret bool, valconf *wrapper.SuperValue) {
	if conf == nil {
		return true, nil
	}

	ret = true
	conf.Range(func(key string, val *wrapper.SuperValue) bool {
		if ok, v := gvfn(key); ok {
			strv := utils.GetStrValue(v, "")
			matchV := val.StringByName("value", "")
			if matchV == VAR_NOT_EMPTY {
				if len(strv) == 0 {
					ret = false
					valconf = val
					return false
				}
			} else if matchV == strv || strings.Contains(matchV+",", strv+",") {
				return true
			} else {
				ret = false
				valconf = val
				return false
			}
			return true
		} else {
			ret = false
			valconf = val
			return false
		}
	})
	return
}
